<template>
  <section>
    <!--#region 搜索条-->

    <xt-search @search="handleSearch" :searchFlag="getOperation('list')">
      <el-input v-model="filters.code" :placeholder="$t('supplier.code')" size="small" class="search-box__input" style="width:230px"
                clearable @keyup.enter.native="handleSearch"></el-input>
      <el-input v-model="filters.name" :placeholder="$t('supplier.name')" size="small" class="search-box__input" style="width:230px"
                clearable @keyup.enter.native="handleSearch"></el-input>
      <el-input v-model="filters.phone" :placeholder="$t('supplier.phone')" size="small" class="search-box__input" style="width:230px"
                clearable @keyup.enter.native="handleSearch"></el-input>
      <el-input v-model="filters.address" :placeholder="$t('supplier.address')" size="small" class="search-box__input" style="width:230px"
                clearable @keyup.enter.native="handleSearch"></el-input>
      <template slot="actions">
        <el-button size="small" class="search-box__button" type="primary" icon="plus" @click="handleAdd"
                   v-if="getOperation('insert')">{{$t('add')}}
        </el-button>
      </template>
    </xt-search>

    <!--#endregion-->

    <!--#region 页面主要列表-->

    <!--列表-->

    <el-table :data="supplierList" highlight-current-row v-loading="loading.listLoading" style="width: 100%;" stripe
              border>
      <el-table-column align="left" header-align="center" type="index" width="60" :label="$t('index')">
        <template slot-scope="scope">
          {{(pageInfo.pageIndex-1)*pageInfo.pageSize+scope.$index+1}}
        </template>
      </el-table-column>
      <el-table-column align="left" header-align="center" prop="code" :label="$t('supplier.code')" min-width="100"
                       :show-overflow-tooltip="true"></el-table-column>
      <!-- <el-table-column align="left" header-align="center" prop="shortName" :label="$t('supplier.shortName')" min-width="100"
                        :show-overflow-tooltip="true"></el-table-column>-->
      <el-table-column align="left" header-align="center" prop="name" :label="$t('supplier.name')" min-width="250"
                       :show-overflow-tooltip="true"></el-table-column>
      <el-table-column align="left" header-align="center" prop="director" :label="$t('supplier.director')" min-width="80"
                       :show-overflow-tooltip="true"></el-table-column>
      <el-table-column align="left" header-align="center" prop="tel" :label="$t('supplier.tel')" min-width="100"
                       :show-overflow-tooltip="true"></el-table-column>
      <el-table-column align="left" header-align="center" prop="phone" :label="$t('supplier.phone')" min-width="100"
                       :show-overflow-tooltip="true"></el-table-column>
      <el-table-column align="left" header-align="center" prop="address" :label="$t('supplier.address')" min-width="100"
                       :show-overflow-tooltip="true"></el-table-column>
      <!--<el-table-column align="left" header-align="center" prop="taxId" :label="$t('supplier.taxId')" min-width="100"
                       :show-overflow-tooltip="true"></el-table-column>-->
      <el-table-column align="left" header-align="center" prop="mail" :label="$t('supplier.mail')" min-width="100"
                       :show-overflow-tooltip="true"></el-table-column>
      <!--<el-table-column align="left" header-align="center" prop="site" :label="$t('supplier.site')" min-width="100"
                       :show-overflow-tooltip="true"></el-table-column>-->
      <!--<el-table-column align="left" header-align="center" prop="remark" :label="$t('supplier.remark')" min-width="100"
                       :show-overflow-tooltip="true"></el-table-column>-->
      <el-table-column align="left" header-align="center" :label="$t('operation')" width="150" v-if="getOperation('update|delete')">
        <template slot-scope="scope">
          <el-button size="mini" type="warning" @click="handleEdit(scope.row, scope.$index)"
                     v-if="getOperation('update')">{{$t('modify')}}
          </el-button>
          <el-button type="danger" size="mini" @click="deleteData(scope.row, scope.$index)"
                     v-if="getOperation('delete')">{{$t('delete')}}
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!--#endregion-->

    <!--#region 工具条-->

    <!--工具条-->

    <el-row>
      <el-col :span="24" class="toolbar">
        <el-pagination
          style="float:right;"
          background
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="pageInfo.pageIndex"
          :page-sizes="[10, 20, 30, 40]"
          :page-size="pageInfo.pageSize"
          layout="sizes, prev, pager, next"
          :total="pageInfo.count">
        </el-pagination>
      </el-col>
    </el-row>

    <!--#endregion-->

    <!--#region 新增修改页面-->

    <!--新增界面-->

    <el-dialog width="95%" top="20px" :title="formData.id==0?$t('add'):$t('modify')" :visible.sync="dialog.formDataVisible"
               :close-on-click-modal="false" :modal-append-to-body="false">
      <el-form :model="formData" class="main-table" label-width="100px" :rules="formRules" ref="formData">
        <el-form-item :label="$t('supplier.code')" prop="code">
          <el-input v-model="formData.code" auto-complete="off" :maxlength="50" :placeholder="$t('supplier.validateCode')"
                    size="small"></el-input>
        </el-form-item>
        <el-form-item :label="$t('supplier.name')" prop="name">
          <el-input v-model="formData.name" auto-complete="off" :maxlength="50" :placeholder="$t('supplier.validateName')"
                    size="small"></el-input>
        </el-form-item>
        <el-form-item :label="$t('supplier.shortName')" prop="shortName">
          <el-input v-model="formData.shortName" auto-complete="off" :maxlength="20" :placeholder="$t('supplier.validateShortName')"
                    size="small"></el-input>
        </el-form-item>
        <el-form-item :label="$t('supplier.director')" prop="director">
          <el-input v-model="formData.director" auto-complete="off" :maxlength="20" :placeholder="$t('supplier.validateDirector')"
                    size="small"></el-input>
        </el-form-item>
        <el-form-item :label="$t('supplier.tel')" prop="tel">
          <el-input v-model="formData.tel" auto-complete="off" :maxlength="20" :placeholder="$t('supplier.validateTel')"
                    size="small"></el-input>
        </el-form-item>
        <el-form-item :label="$t('supplier.phone')" prop="phone">
          <el-input v-model="formData.phone" auto-complete="off" :maxlength="20" :placeholder="$t('supplier.validatePhone')"
                    size="small"></el-input>
        </el-form-item>
        <el-form-item :label="$t('supplier.address')" prop="address">
          <el-input v-model="formData.address" auto-complete="off" :maxlength="50" :placeholder="$t('supplier.validateAddress')"
                    size="small"></el-input>
        </el-form-item>
        <el-form-item :label="$t('supplier.taxId')" prop="taxId">
          <el-input v-model="formData.taxId" auto-complete="off" :maxlength="20" :placeholder="$t('supplier.validateTaxId')"
                    size="small"></el-input>
        </el-form-item>
        <el-form-item :label="$t('supplier.mail')" prop="mail">
          <el-input v-model="formData.mail" auto-complete="off" :maxlength="50" :placeholder="$t('supplier.validateMail')"
                    size="small"></el-input>
        </el-form-item>
        <el-form-item :label="$t('supplier.site')" prop="site">
          <el-input v-model="formData.site" auto-complete="off" :maxlength="50" :placeholder="$t('supplier.validateSite')"
                    size="small"></el-input>
        </el-form-item>
        <el-form-item :label="$t('supplier.fax')" prop="fax">
          <el-input v-model="formData.fax" auto-complete="off" :maxlength="20" :placeholder="$t('supplier.validateFax')"
                    size="small"></el-input>
        </el-form-item>
        <el-form-item :label="$t('supplier.depositBank')" prop="depositBank">
          <el-input v-model="formData.depositBank" auto-complete="off" :maxlength="20" :placeholder="$t('supplier.validateDepositBank')"
                    size="small"></el-input>
        </el-form-item>
        <el-form-item :label="$t('supplier.bankAccount')" prop="bankAccount">
          <el-input v-model="formData.bankAccount" auto-complete="off" :maxlength="20" :placeholder="$t('supplier.validateBankAccount')"
                    size="small"></el-input>
        </el-form-item>
        <el-form-item :label="$t('supplier.credit')" prop="credit">
          <el-input v-model="formData.credit" auto-complete="off" :maxlength="20" :placeholder="$t('supplier.validateCredit')"
                    size="small"></el-input>
        </el-form-item>
        <el-form-item :label="$t('supplier.remark')" prop="remark" style="width: 100%">
          <el-input v-model="formData.remark" auto-complete="off" :maxlength="255" :placeholder="$t('supplier.validateRemark')" size="small"
                    type="textarea" resize="none"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="handleCancel">{{$t('cancel')}}</el-button>
        <el-button type="primary" @click.native="submitData" :loading="loading.addLoading">{{$t('submit')}}</el-button>
      </div>
    </el-dialog>

    <!--#endregion-->

  </section>
</template>

<script>

  /*#region 导入函数*/

  import PagePlugIn from "../../../mixins/page-plug-in";
  import {SupplierAPI} from "../../../modules";
  import {GetExitData} from "../../../utils/utils";

  /*#endregion*/

  export default {
    name: "Supplier",
    methods: {

      /*#region 自定义函数(组件change事件等)*/

      /*#endregion*/

      /*#region 数据提交事件*/

      //提交事件

      submitData() {
        this.$refs.formData.validate((valid) => {
          if (valid) {
            const params = JSON.parse(JSON.stringify(this.formData));
            if (params.id === 0) {
              this.loading.addLoading = true;
              SupplierAPI.insertSupplier(params).then(({data}) => {
                if (data.code === 1) {
                  this.$refs.formData.resetFields();
                  this.getMainList();
                  this.dialog.formDataVisible = false;
                }
                this.loading.addLoading = false;
              });
            } else {
              this.loading.addLoading = true;
              SupplierAPI.updateSupplier(params).then(({data}) => {
                if (data.code === 1) {
                  this.$refs.formData.resetFields();
                  this.getMainList();
                  this.dialog.formDataVisible = false;
                }
                this.loading.addLoading = false;
              });
            }
          }
        });
      },

      //删除

      deleteData(row) {
        this.$confirm(this.$t('deleteConfirm'), this.$t('tips'), {
          confirmButtonText: this.$t('confirm'),
          cancelButtonText: this.$t('cancel'),
          type: "warning"
        }).then(() => {
          SupplierAPI.deleteSupplier({id: row.id}).then(({data}) => {
            if (data.code === 1) {
              this.getMainList();
            }
          });
        });
      },


      /*#endregion*/

      /*#region 操作函数(handle)*/

      //修改

      handleEdit(row) {
        this.$refs.formData && this.$refs.formData.resetFields();
        this.formData = JSON.parse(JSON.stringify(row));
        this.dialog.formDataVisible = true;
      },
      handleCancel() {
        this.dialog.formDataVisible = false;
      },
      handleAdd() {
        this.$refs.formData && this.$refs.formData.resetFields();
        this.formData = SupplierAPI.init();
        this.dialog.formDataVisible = true;
      },

      /*#endregion*/

      /*#region 获取数据*/

      getMainList() {
        const params = {
          ...this.getParams(),
          orderBy: "Id desc"
        };
        SupplierAPI.listSupplier(params).then(({data, list}) => {
          this.supplierList = list;
          this.pageInfo.pageIndex = data.data ? data.data.currentPage : 1;
          this.pageInfo.count = data.data ? data.data.count : 0;
        });
      }

      /*#endregion*/

    },

    /*#region 生命周期函数(created、mount、等)*/

    created() {
      this.getMainList();
    },

    /*#endregion*/

    /*#region 注册绑定数据*/

    mixins: [PagePlugIn],
    data() {
      const checkCode = (rule, value, callback) => {
        GetExitData({code: value}, SupplierAPI.listSupplier).then(({isEmpty, id}) => {
          if ((id === this.formData.id)) {callback();} else {
            if (!isEmpty) {return callback(new Error(this.$t('supplier.exitsCode')));} else {callback();}
          }
        });
      };
      const checkName = (rule, value, callback) => {
        GetExitData({name: value}, SupplierAPI.listSupplier).then(({isEmpty, id}) => {
          if ((id === this.formData.id)) {callback();} else {
            if (!isEmpty) {return callback(new Error(this.$t('supplier.exitsName')));} else {callback();}
          }
        });
      };
      return {
        filters: SupplierAPI.init(),
        formData: SupplierAPI.init(),
        formRules: {
          code: [{required: true, message: this.$t('supplier.validateCode')}, {validator: checkCode, trigger: "blur"}],
          name: [{required: true, message: this.$t('supplier.validateName')}, {validator: checkName, trigger: "blur"}],
          mail: [{type: "email", message: this.$t('supplier.correctEmail')}]
        },
        dialog: {formDataVisible: false},
        loading: {addLoading: false, listLoading: false},
        supplierList: []
      };
    }

    /*#endregion */

  };
</script>

<style scoped lang="scss">
  .main-table {
    .el-form-item {
      width: 33%;
      display: inline-block;
    }

  }
</style>
